
export const getDeviceInfo = () => {

    function getDeviceId() {
        return Date.now() + "" + Math.floor(Math.random() * 1e7);
    }

    function getTheme() {
        try {
            return window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
        } catch (error) {
            return "light";
        }
    }

    const userAgent = navigator.userAgent;
    const isAndroid = /* @__PURE__ */ /android/i.test(userAgent);
    const isIOS$1 = /* @__PURE__ */ /iphone|ipad|ipod/i.test(userAgent);
    const isWindows = /* @__PURE__ */ userAgent.match(/Windows NT ([\d|\d.\d]*)/i);
    const isMac = /* @__PURE__ */ /Macintosh|Mac/i.test(userAgent);
    const isLinux = /* @__PURE__ */ /Linux|X11/i.test(userAgent);
    const isIPadOS = isMac && navigator.maxTouchPoints > 0;

    const isFunction = (val: any) => typeof val === "function";

    function IEVersion() {
        const userAgent = navigator.userAgent;
        const isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
        const isEdge = userAgent.indexOf("Edge") > -1 && !isIE;
        const isIE11 = userAgent.indexOf("Trident") > -1 && userAgent.indexOf("rv:11.0") > -1;
        if (isIE) {
            const reIE = new RegExp("MSIE (\\d+\\.\\d+);");
            reIE.test(userAgent);
            const fIEVersion = parseFloat(RegExp.$1);
            if (fIEVersion > 6) {
                return fIEVersion;
            } else {
                return 6;
            }
        } else if (isEdge) {
            return -1;
        } else if (isIE11) {
            return 11;
        } else {
            return -1;
        }
    }

    let osName;
    let osVersion = "0";
    let deviceModel = "";
    let deviceType = "phone";
    const osLanguage = navigator.language;
    if (isIOS$1) {
        osName = "iOS";
        const osVersionFind = userAgent.match(/OS\s([\w_]+)\slike/);
        if (osVersionFind) {
            osVersion = osVersionFind[1].replace(/_/g, ".");
        }
        const deviceModelFind = userAgent.match(/\(([a-zA-Z]+);/);
        if (deviceModelFind) {
            deviceModel = deviceModelFind[1];
        }
    } else if (isAndroid) {
        osName = "Android";
        const osVersionFind = userAgent.match(/Android[\s/]([\w\.]+)[;\s]/);
        if (osVersionFind) {
            osVersion = osVersionFind[1];
        }
        const infoFind = userAgent.match(/\((.+?)\)/);
        const infos = infoFind ? infoFind[1].split(";") : userAgent.split(" ");
        const otherInfo = [
            /\bAndroid\b/i,
            /\bLinux\b/i,
            /\bU\b/i,
            /^\s?[a-z][a-z]$/i,
            /^\s?[a-z][a-z]-[a-z][a-z]$/i,
            /\bwv\b/i,
            /\/[\d\.,]+$/,
            /^\s?[\d\.,]+$/,
            /\bBrowser\b/i,
            /\bMobile\b/i
        ];
        for (let i = 0; i < infos.length; i++) {
            const info = infos[i];
            if (info.indexOf("Build") > 0) {
                deviceModel = info.split("Build")[0].trim();
                break;
            }
            let other;
            for (let o2 = 0; o2 < otherInfo.length; o2++) {
                if (otherInfo[o2].test(info)) {
                    other = true;
                    break;
                }
            }
            if (!other) {
                deviceModel = info.trim();
                break;
            }
        }
    } else if (isIPadOS) {
        deviceModel = "iPad";
        osName = "iOS";
        deviceType = "pad";
        osVersion = isFunction(window.BigInt) ? "14.0" : "13.0";
    } else if (isWindows || isMac || isLinux) {
        deviceModel = "PC";
        osName = "PC";
        deviceType = "pc";
        osVersion = "0";
        // @ts-ignore
        let osVersionFind = userAgent.match(/\((.+?)\)/)[1];
        if (isWindows) {
            osName = "Windows";
            switch (isWindows[1]) {
                case "5.1":
                    osVersion = "XP";
                    break;
                case "6.0":
                    osVersion = "Vista";
                    break;
                case "6.1":
                    osVersion = "7";
                    break;
                case "6.2":
                    osVersion = "8";
                    break;
                case "6.3":
                    osVersion = "8.1";
                    break;
                case "10.0":
                    osVersion = "10";
                    break;
            }
            const framework = osVersionFind && osVersionFind.match(/[Win|WOW]([\d]+)/);
            if (framework) {
                osVersion += ` x${framework[1]}`;
            }
        } else if (isMac) {
            osName = "macOS";
            const _osVersion = osVersionFind && osVersionFind.match(/Mac OS X (.+)/) || "";
            if (osVersion) {
                osVersion = _osVersion[1].replace(/_/g, ".");
                if (osVersion.indexOf(";") !== -1) {
                    osVersion = osVersion.split(";")[0];
                }
            }
        } else if (isLinux) {
            osName = "Linux";
            const _osVersion = osVersionFind && osVersionFind.match(/Linux (.*)/) || "";
            if (_osVersion) {
                osVersion = _osVersion[1];
                if (osVersion.indexOf(";") !== -1) {
                    osVersion = osVersion.split(";")[0];
                }
            }
        }
    } else {
        osName = "Other";
        osVersion = "0";
        deviceType = "unknown";
    }
    const system = `${osName} ${osVersion}`;
    const platform = osName.toLocaleLowerCase();
    let browserName = "";
    let browserVersion = String(IEVersion());
    if (browserVersion !== "-1") {
        browserName = "IE";
    } else {
        const browseVendors = ["Version", "Firefox", "Chrome", "Edge{0,1}"];
        const vendors = ["Safari", "Firefox", "Chrome", "Edge"];
        for (let index2 = 0; index2 < browseVendors.length; index2++) {
            const vendor = browseVendors[index2];
            const reg = new RegExp(`(${vendor})/(\\S*)\\b`);
            if (reg.test(userAgent)) {
                browserName = vendors[index2];
                // @ts-ignore
                browserVersion = userAgent.match(reg)[2];
            }
        }
    }
    let deviceOrientation = "portrait";
    const orientation = typeof window.screen.orientation === "undefined" ? window.orientation : window.screen.orientation.angle;
    deviceOrientation = Math.abs(orientation) === 90 ? "landscape" : "portrait";
    return {
        deviceId: getDeviceId(),
        deviceType,
        deviceBrand: void 0,
        deviceModel,
        deviceOrientation,
        osName,
        osVersion,
        osLanguage,
        osTheme: getTheme(),
        browserName: browserName.toLocaleLowerCase(),
        browserVersion,
        platform,
        userAgent,
        system,
    };
}
